package com.wangtianxiang.rabbitmq;

import com.alibaba.fastjson.JSON;
import com.wangtianxiang.rabbitmq.config.RabbitMqConfig;
import com.wangtianxiang.rabbitmq.domain.Order;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.rabbit.connection.SimpleResourceHolder;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Date;

@SpringBootTest(classes = BootApplication.class)
@RunWith(SpringRunner.class)
public class Send {
    @Autowired
    RabbitTemplate rabbitTemplate;

    @Test
    public void sendMsgByTopics(){
        Date date = new Date();
        Order order = new Order("13232312","广东省",date,date);
        SimpleResourceHolder.bind(rabbitTemplate.getConnectionFactory(),"business1");
        rabbitTemplate.convertAndSend(RabbitMqConfig.EXCHANGE_NAME,"topic.sms.email",JSON.toJSONString(order));
//        rabbitTemplate.convertAndSend("test_fanout_exchange","topic.sms.email",JSON.toJSONString(order));
  /*      for (int i = 0; i < 5;i++){
            String message = "恭喜你,注册成功! userId=" +i;
            rabbitTemplate.convertAndSend(RabbitmqConfig.EXCHANGE_NAME,"topic.sms.email",message);
            System.out.println(" [x] Sent '" + message + "'");
        }*/
    }
}
